package com.vitaf.cash.app.ui.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vitaf.cash.app.R
import com.vitaf.cash.app.event.VitafEventPointType
import com.vitaf.cash.app.request.HttpHelper
import com.vitaf.cash.app.ui.popupwindow.VitafGeneralPopup
import com.vitaf.cash.app.utils.VitafDensityUtil
import com.vitaf.cash.app.utils.decoration.LinearSpacingItemDecoration
import com.vitaf.cash.lib.adapter.VitafRecyclerViewAdapter
import com.vitaf.cash.lib.adapter.VitafViewHolderHelper
import org.json.JSONObject


/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：流程 - 联系人适配器
 */
class VitafStepContactApt(private val parentView:ViewGroup?, recyclerView: RecyclerView?) :
    VitafRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.vitaf_fmt_step_contact_item) {

    /**
     * 选择联系电话监听
     */
    private var chooseMobileListsner: IChildItemSelectMobileListener? = null
    fun setOnChildItemSelectMobileListener(listener: IChildItemSelectMobileListener){
        chooseMobileListsner = listener
    }

    override fun fillData(helper: VitafViewHolderHelper, position: Int, model: JSONObject) {
        val txtAuthenticationContactIndex = helper.getTextView(R.id.txtAuthenticationContactIndex)
        var showContactIndex = String.format(mContext.getString(R.string.gombe_credit_process_step_contact_index),"${position+1}")
        if (position > 0){
            showContactIndex = "$showContactIndex ${mContext.getString(R.string.gombe_credit_process_step_contact_different)}"
        }
        txtAuthenticationContactIndex.text = showContactIndex

        val recyclerViewContact = helper.getView<RecyclerView>(R.id.recyclerViewContact)
        recyclerViewContact.setHasFixedSize(true)
        recyclerViewContact.isNestedScrollingEnabled = false
        recyclerViewContact.isFocusableInTouchMode = false //不需要焦点
        recyclerViewContact.layoutManager = LinearLayoutManager(mContext)
        if (recyclerViewContact.itemDecorationCount == 0){
            val topDecoration = VitafDensityUtil.dp2px(16f)
            recyclerViewContact.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        }
        val adapter = VitafStepGeneralApt(recyclerViewContact)
        recyclerViewContact.adapter = adapter
        recyclerViewContact.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "CONTACT_GROUP_RELATION" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null) {
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView, dataList) { data ->

                        //选择的Other需要设置为空
                        data?.put("otherValue","")
                        childModel.put("selectItem", data)
                        adapter.notifyItemChanged(childPosition)

                        val subRemark = "${data?.optString("value")}$$${data?.optString("duration")}"
                        HttpHelper.addPoint(String.format(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.STEP_XX_SELECT,"${model.optString("stepTitle")}_${childModel.optString("paramName")}_${position+1}"), subRemark)
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    if (chooseMobileListsner != null){
                        chooseMobileListsner?.onChildSelectMobileClick(position,childModel)
                    }
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                val contactItem = dataArray.optJSONObject(i)
                contactItem.put("stepTitle",model.optString("stepTitle"))
                contactItem.put("stepType","CONTACT")
                contactItem.put("stepItemIndex",position+1)
                dataList.add(contactItem)
            }
        }
        adapter.data = dataList
    }

    /**
     * choose Mobile
     */
    interface IChildItemSelectMobileListener{
        fun onChildSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    /**
     * SELECT
     */
    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject?)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = VitafGeneralPopup(mContext)
        selectPopup.setWindowWidth(view.width + VitafDensityUtil.dp2px(10f))
        selectPopup.setSelectItemListener(object : VitafGeneralPopup.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                listener.onChildSelectClick(data)
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        parentView?.let {
            selectPopup.show(view, it)
            selectPopup.setBrandList(dataList)
        }
    }
}